
import deepCopy from '../../../utils/deep-copy';

import QueryConditionComp from '../components/QueryCondition';

import CustomSearchArea from '../components/CustomSearch';

export default class QueryCondition {

    constructor(comp) {
        this.comp = comp;
        Object.assign(this, comp.props);
    }

    init = () => {
        const {
            reportQuery,
            initLeftTreeData
        } = this.comp.props;
        
        let template = this.meta.getMeta();
        // 表单变成编辑态
        this.form.setFormStatus(reportQuery.currentQueryForm, 'edit');

        // 为了员工台账报表专门执行
        if(reportQuery.currentTreeData === '1001Z71000000000A3GT') {
            this.comp.props.renderEmployeeInput();
            initLeftTreeData();
        }
        

        this.dispatch({
            type: 'reportQuery/update',
            payload: {
                queryLeftData: [],
                queryRightData: [],
                leftChecked: [],
                rightChecked: [],
                hasShowQueryCondition: true
            }
        });
    }

    // 穿梭狂穿梭的时候
    changeTransfer = (leftData, rightData) => {

        this.dispatch({ 
            type: 'reportQuery/update',
            payload: {
                queryLeftData: leftData,
                queryRightData: rightData
            }
        });

    }

    // 表单编辑后事件
    afterFormEdit = (props, moduleId, key, value, oldValue) => {
        let formData = this.form.getAllFormValue(moduleId);
        let postData = {
            area_code: moduleId,
            key: key,
            model: formData
        };

        this.dispatch({
            type: 'reportQuery/afterEditForm',
            payload: {
                postData
            }
        })
            .then((res) => {
                let {form, item_list} = res.data;
                if(form) {
                    this.form.setAllFormValue({
                        [moduleId]: form[moduleId]
                    });
                }
                
                if(item_list && item_list.length > 0) {
                    this.dispatch({
                        type: 'reportQuery/update',
                        payload: {
                            queryLeftData: this.processTransferData(item_list),
                            queryRightData: []
                        }
                    });
                }
            });
    }
    
    // 处理返回的穿梭狂数据
    processTransferData = (data = []) => {

        data.map((item) => {
            item.display = item.title;
        });

        return data;
    }

    // 表单编辑前事件
    beforeFormEdit = async (props, moduleId, key, value, data) => {
        let template = this.meta.getMeta();
        let formData = this.form.getAllFormValue(moduleId);

        let postData = {
            area_code: moduleId,
            key: key,
            model: formData
        };

        try {
            let res = await this.dispatch({
                type: 'reportQuery/beforeEditForm',
                payload: {
                    postData: postData
                }
            })

            let {editTable, refParam = {}} = res.data;
            this.form.setFormItemsDisabled(moduleId, {
                [key]: editTable
            });
            template[moduleId].items.map((item) => {
                if(item.queryCondition && item.attrcode === key) {
                    Object.assign(item.queryCondition, refParam);
                }
            });
        }
        catch(e) {
        }

        return true;
    }

    // 穿梭狂item上下移动
    moveItem = (direction) => {
        return () => {
            const {
                leftChecked, 
                rightChecked,
                queryRightData,
                queryLeftData
            } = this.comp.props.reportQuery;

            let nList = [...queryRightData];
            let prefixList = [];
            let subfixList = [];

            queryRightData.map((item, index) => {
                if(rightChecked.includes(item.key)) {
                    // 向上
                    if(direction === 'up') {
                        if(index > 0) {
                            // 与上一个元素交换
                            nList[index] = nList[index - 1];
                            nList[index - 1] = item;
                        }
                    }
                    else if(direction === 'upp') {
                        // 存到前置数组里，并且置空现在元素
                        prefixList.push(deepCopy(nList[index]));
                        nList[index] = null;
                    }
                    else if(direction === 'down') {
                        // 与下一个元素交换
                        if(index < queryRightData.length - 1) {
                            nList[index] = nList[index + 1];
                            nList[index + 1] = item;
                        }
                    }
                    else if(direction === 'downn') {
                        // 存到后置数组里，并且置空当前元素
                        subfixList.push(deepCopy(nList[index]));
                        nList[index] = null;
                    }
                }
            });
            // 删除null元素
            nList = nList.filter(item => item);
            // 合并前缀数组
            if(prefixList.length > 0) {
                nList = prefixList.concat(nList);
            }
            // 合并后缀数组
            if(subfixList.length > 0) {
                nList = nList.concat(subfixList);
            }

            this.dispatch({
                type: 'reportQuery/update',
                payload: {
                    queryRightData: nList
                }
            });
        }
    }

    // 穿梭狂选中
    // 这个穿梭狂是我自己写的，比较变态，checkedList里的元素的status为true才是选中
    checkedTransfer = (type, checkedList) => {
        
        let nList = [];

        Object.keys(checkedList).map((key) => {
            if(checkedList[key].status) {
                nList.push(checkedList[key].data.key)
            }
        })

        let payload = {};
        if(type === 'left') {
            payload = {
                leftChecked: nList
            };
        }
        else {
            payload = {
                rightChecked: nList
            }
        }
        this.dispatch({
            type: 'reportQuery/update',
            payload: payload
        });
    }

    // 如果有form 就渲染querycondition
    renderSearchTab = () => {
        const {currentQueryForm, language} = this.comp.props.reportQuery;
        if(!currentQueryForm) {
            return null;
        }
        else {
            return () => {
                return [{
                    name: language['hrpub-000140'], // '查询条件2',
                    content: (
                        <QueryConditionComp
                            {...this.comp.props}
                            initLeftTreeData={this.comp.action.ReportQuery.initLeftTreeData}
                            searchMan={this.comp.action.ReportQuery.searchMan}
                            renderEmployeeInput={this.comp.action.ReportQuery.renderEmployeeInput}
                        />
                    )
                }]
            }
        }
    }

    renderCustomSearchBody = () => {
        let {
            customFormData,
            language,
            customFormValue,
            customReferValue,
            customQueryCondition,
            currentTreeData,
            refQueryConditionMap
        } = this.comp.props.reportQuery;

        return (
            <CustomSearchArea
                language={language}
                data={customFormData}
                onChange={this.onChangeCustomSearchArea}
                customFormValue={customFormValue}
                customReferValue={customReferValue}
                ref={ref => this.customSearchRef = ref}
                dispatch={this.dispatch}
                customQueryCondition={customQueryCondition}
                currentTreeData={currentTreeData}
                refQueryConditionMap={refQueryConditionMap}
            />
        );
    }

    // 自定义的查询区域的change handle
    onChangeCustomSearchArea = (type, item) => {
        return (...args) => {
            let {reportQuery} = this.comp.props;
            let value = '';

            if(type === 'select') {
                value = args[0]
            }
            else if(type === 'refer') {
                let sourceValue = {
                    refpk: [],
                    refname: [],
                    refcode: []  
                };
                if(Array.isArray(args[0])) {
                    args[0].map((v) => {
                        sourceValue.refpk.push(v.refpk);
                        sourceValue.refname.push(v.refname);
                        sourceValue.refcode.push(v.refcode);
                    });

                }
                else {
                    sourceValue.refpk.push(args[0].refpk);
                    sourceValue.refname.push(args[0].refname);
                    sourceValue.refcode.push(args[0].refcode);
                }
                
                if(/6/.test(item.dataType)) {
                    value = sourceValue.refname.join(',');
                }
                else if(/4/.test(item.dataType)) {
                    value = sourceValue.refpk.join(',');
                }
                else if(/5/.test(item.dataType)) {
                    value = sourceValue.refcode.join(',');
                }

                if(value.split(',').length > 1) {
                    value = `(${value})`;
                }
            }
            else if(type === 'input') {
                value = args[0]
            }
            else if(type === 'date') {
                value = args[0]
            }
            let payload = {
                customFormValue: {
                    ...reportQuery.customFormValue,
                    [item.code]: value
                }
            };

            // 编制管理的编制纬度参照需要添加参数
            if(item.code === 'B5:O6year') {
                payload['customQueryCondition'] = {
                    ...reportQuery.queryCondition,
                    year: value
                };
            }

            if(type === 'refer') {
                payload['customReferValue'] = {
                    ...reportQuery.customReferValue,
                    [item.code]: args[0]
                };
            }

            this.dispatch({
                type: 'reportQuery/update',
                payload: payload
            });
        }
    }

}